package com.q.tqb.activity.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.BaseInfo;
import com.q.tqb.model.UserInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.TimeButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ResetPassWordActivity extends BaseActivity {
    @BindView(R.id.et_tel)
    public EditText et_tel;

    @BindView(R.id.et_code)
    public EditText et_code;

    @BindView(R.id.et_pass)
    public EditText et_pass;

    @BindView(R.id.et_pass1)
    public EditText et_pass1;

    @BindView(R.id.btn_code)
    public TimeButton btn_code;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_pass_word;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("密码重置");

    }

    @OnClick({R.id.iv_back, R.id.tv_login, R.id.btn_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_code:
                getPhoneCode();
                break;
            case R.id.tv_login:
                resetPassWord();
                break;
        }
    }

    private void getPhoneCode() {
        String phone = et_tel.getText().toString().trim();
        if (!StringUtil.isMobile(phone)) {
            showToast("请输入正确的手机号");
            return;
        }
        btn_code.startTimer();
        Map<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("type", "7");
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_GET_PHONE_CODE, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                BaseInfo personData = gson.fromJson(result, BaseInfo.class);
                showToast(personData.getMsg());
                mProgressDialog.dismiss();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

    private void resetPassWord() {
        if (StringUtil.isMobile(et_code.getText().toString().trim())) {
            showToast("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(et_pass.getText().toString().trim())) {
            showToast("请输入密码");
            return;
        }
        if (et_pass.getText().length() < 6) {
            showToast("密码不能小于6位");
            return;
        }
        if (TextUtils.isEmpty(et_pass1.getText().toString().trim())) {
            showToast("请输入确认密码");
            return;
        }
        if (!et_pass1.getText().toString().equals(et_pass.getText().toString())) {
            showToast("两次密码不一致，请重新输入");
            return;
        }
        Map<String, String> map = new HashMap<>();
        final String mobile = et_tel.getText().toString().trim();
        final String pwd = et_pass.getText().toString().trim();
        String pwd1 = et_pass1.getText().toString().trim();
        map.put("mobile", mobile);
        map.put("password", pwd);
        map.put("confirm_password", pwd1);
        map.put("code", et_code.getText().toString().trim());
        mProgressDialog.show();

        OkHttpUtil.getInstance().post(this, AppConst.URL_FIND_PWD, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                BaseInfo personData = gson.fromJson(result, BaseInfo.class);
                showToast(personData.getMsg());
                mProgressDialog.dismiss();
                reSetOk(mobile, pwd);
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

    private void reSetOk(String mobile, String pwd) {
        Intent intent = new Intent();
        intent.putExtra("mobile", mobile);
        intent.putExtra("pwd", pwd);
        setResult(RESULT_OK, intent);
        this.finish();
    }
}
